//
//  import.cpp
//  
//
//  Created by Nikita Luzyanin on 30.11.13.
//
//

#include <iostream>
#include <fstream>
#include <iomanip>
#include <stdlib.h>
#include "functions.cpp"
using namespace std;

/*int moveFolder()
{
    char *pathToFS;
    pathToFS = argv[1];
    FILE *f;
    f = fopen(pathToFS, "rb");
    if (f == NULL) {
        cout << "Path to FS does not exist\n";
        return 1;
    }
    fclose(f);
    
    char pathToRoot[1000];
    strcpy(pathToRoot, pathToFS);
    strcat(pathToRoot, "/0");
    f = fopen(pathToRoot, "rb");
    if (f == NULL) {
        cout << "FS is not formatted\n";
        return 1;
    }
    fclose(f);
    
    char pathToSrc[1000], pathToDst[1000];
    strcpy(pathToSrc, argv[2]);
    strcpy(pathToDst, argv[3]);
    
    char srcMetaBlock[10], srcContentBlock[10];
    if (findMetaBlock(pathToFS, srcMetaBlock, pathToSrc, false, true, false) != 0) {
        cout << "No File in FS\n";
        return 1;
    }
    if (findContentBlock(pathToFS, srcMetaBlock, srcContentBlock) != 0) {
        cout << "No Content File\n";
        return 1;
    }
    char pathToSrcContentBlock[1000], pathToSrcMetaBlock[1000], helper[20];
    strcpy(pathToSrcContentBlock, pathToFS);
    strcat(pathToSrcContentBlock, srcContentBlock);
    strcpy(pathToSrcMetaBlock, pathToFS);
    strcat(pathToSrcMetaBlock, srcMetaBlock);
    
    char dstMetaBlock[10], dstContentBlock[10];
    if (findMetaBlock(pathToFS, dstMetaBlock, pathToDst, true, true, false) != 0) {
        cout << "Mistake\n";
        return 1;
    }
    if (findContentBlock(pathToFS, dstMetaBlock, dstContentBlock) != 0) {
        cout << "Mistake\n";
        return 1;
    }
    char pathToDstContentBlock[1000], pathToDstMetaBlock[1000];
    strcpy(pathToDstContentBlock, pathToFS);
    strcat(pathToDstContentBlock, dstContentBlock);
    strcpy(pathToDstMetaBlock, pathToFS);
    strcat(pathToDstMetaBlock, dstMetaBlock);
    
    char contentName[12], pathToContent[1000], ch, nextSrcContentBlock[10], nextDstContentBlock[10], newDstContentBlock[10];
    
    fstream srcB, dstB;
    srcB.open(pathToSrcMetaBlock);
    srcB >> helper;
    int currentPositionSrcB = srcB.tellg();
    srcB >> newDstContentBlock;
    char root[10];
    srcB >> root;
    int size;
    srcB >> size;
    srcB.seekp(currentPositionSrcB + 1);
    if (findFreeBlock(pathToFS, nextDstContentBlock, true) != 0) {
        cout << "No more free blocks\n";
    }
    srcB << nextDstContentBlock;
    srcB << "\n";
    srcB << root;
    srcB << " ";
    changeSizeAndDate(pathToFS, srcMetaBlock, 0 - size, false);
    srcB.close();
    
    strcpy(pathToDstContentBlock, pathToFS);
    strcat(pathToDstContentBlock, nextDstContentBlock);
    srcB.open(pathToDstContentBlock);
    srcB << "n\n";
    srcB.close();
    
    changeSizeAndDate(pathToFS, dstMetaBlock, size, false);
    
    return 0;
}*/

int main(int argc, char* argv[])
{
    char *pathToFS;
    pathToFS = argv[1];
    FILE *f;
    f = fopen(pathToFS, "rb");
    if (f == NULL) {
        cout << "Path to FS does not exist\n";
        return 1;
    }
    fclose(f);
    
    char pathToRoot[1000];
    strcpy(pathToRoot, pathToFS);
    strcat(pathToRoot, "/0");
    f = fopen(pathToRoot, "rb");
    if (f == NULL) {
        cout << "FS is not formatted\n";
        return 1;
    }
    fclose(f);
    
    char pathToSrc[1000], pathToDst[1000], pathToDlt[1000];
    strcpy(pathToSrc, argv[2]);
    strcpy(pathToDlt, argv[2]);
    strcpy(pathToDst, argv[3]);
    
    
    if (copySrcToDst(pathToFS, pathToSrc, pathToDst) != 0) {
        cout << "Mistake\n";
        return 1;
    }
    if (remove(pathToFS, pathToDlt) != 0) {
        cout << "Mistake\n";
        return 1;
    }
    
    return 0;
}